package hos.okhttp.convert;

import java.io.IOException;
import java.lang.reflect.Type;

import hos.http.HttpLogUtils;
import hos.http.HttpRequest;
import hos.okhttp.utils.OkHttpResponseUtils;
import okhttp3.Response;

/**
 * <p>Title: JsonConvert </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-02-16 22:51
 */
public abstract class HttpResponseJsonDataConverter extends HttpResponseJsonConverter {

    String data = "data";

    public HttpResponseJsonDataConverter() {
        this("data");
    }

    public HttpResponseJsonDataConverter(String data) {
        super();
        this.data = data;
    }

    public HttpResponseJsonDataConverter(String success, String code, String message) {
        super(success, code, message);
    }

    public HttpResponseJsonDataConverter(String success, String code) {
        super(success, code);
    }

    public HttpResponseJsonDataConverter(String success, String code, String data, String message) {
        super(success, code, message);
        this.data = data;
    }

    @Override
    public <R> R parseJson(HttpRequest<?> request, Type succeed, Response response) throws IOException {
        String json = OkHttpResponseUtils.requireJsonData(response, code, message, success, data);
        HttpLogUtils.responseLog(request, json);
        return parseBody(succeed, response, json);
    }
}
